package com.jfm.jfmc.core.icc.worker;

import com.jfm.jfmc.core.icc.CollectTaskManage;
import com.jfm.jfmm.collector.entity.collect.CollectParam;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author DYang
 * @since 2024-10-22 22:11
 */
public class CollectWorkerFactory {

    /**
     * 采集任务线程池，减少创建线程的开销
     * key: metricId
     */
    private static final Map<String,CollectWorker> collectWorkerMap = new ConcurrentHashMap<>(1024);

    /**
     * 创建采集线程
     * @param collectParam 采集参数
     */
    public static CollectWorker createWorker(CollectParam collectParam){
        if (collectParam == null || collectParam.getParam() == null) {
            return null;
        }
        CollectWorker collectWorker = null;
        String metricId = collectParam.getParam().getMetricId();
        collectWorker = collectWorkerMap.get(metricId);
        if (collectWorker == null) {
            collectWorker = new CollectWorker(collectParam);
            collectWorkerMap.put(metricId,collectWorker);
        }
        return collectWorker;
    }

    /**
     * 根据metricId获取采集任务
     * @param metricId   任务ID
     * @return
     */
    public static CollectWorker createWorker(String metricId) {
        CollectParam collectParam = CollectTaskManage.getCollectParam(metricId);
        return createWorker(collectParam);
    }

}
